<template>
  <div class="detail-container">
    <el-form ref="formRef" :model="form" label-width="80px" :rules="formRules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" :disabled="isEdit"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" :disabled="isEdit"></el-input>
      </el-form-item>
      <!-- 部门级联选择器 -->
      <el-form-item label="部门" prop="departmentId">
        <DepartmentCom v-model="form.departmentId"></DepartmentCom>
      </el-form-item>
      <!-- 聘用形式下拉框 -->
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择" clearable>
          <el-option
            label="正式"
            :value="1"
          >
          </el-option>
          <el-option
            label="非正式"
            :value="2"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- 入职日期 -->
      <el-form-item label="入职日期" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
          clearable
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <!-- 转正日期 -->
      <el-form-item label="转正日期" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
          clearable
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <!-- 头像上传 -->
      <el-form-item label="员工头像">
        <UploadUserPhoto v-model="form.staffPhoto"></UploadUserPhoto>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import DepartmentCom from '@/views/employee/components/departmentCom.vue'
import { addEmployeeRequest, getEmployeeRequest, updateEmployeeRequest } from '@/api/employee'
import UploadUserPhoto from '@/views/employee/components/uploadUserPhoto.vue'
export default {
  components: {
    DepartmentCom,
    UploadUserPhoto
  },
  data() {
    // 日期校验
    const validatorCorrectionTime = (rule, value, callback) => {
      +new Date(this.form.timeOfEntry) > +new Date(this.form.correctionTime) ? callback(new Error('转正期日不能小于入职日期')) : callback()
    }
    return {
      form: {
        username: '',
        mobile: '',
        formOfEmployment: null,
        workNumber: '',
        departmentId: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },
      formRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '名称必须1-4个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '日期不能为空', trigger: 'change' },
          {
            validator: validatorCorrectionTime, trigger: 'change'
          }
        ],
        timeOfEntry: [
          { required: true, message: '日期不能为空', trigger: 'change' },
          {
            validator: validatorCorrectionTime, trigger: 'change'
          }
        ]

      },

      value: []
    }
  },
  computed: {
    isEdit() {
      return !!this.$route.params.id
    }
  },
  async created() {
    if (this.$route.params.id) {
      const res = await getEmployeeRequest(this.$route.params.id)
      this.form = res.data
    }
  },
  methods: {

    // 表单提交
    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await updateEmployeeRequest(this.form)
            } else {
              await addEmployeeRequest(this.form)
            }
            this.$router.back()
          } catch (error) {
          //
          }
        }
      })
    },
    // 重置表单
    resetForm() {
      this.$refs.formRef.resetFields()
      this.form.staffPhoto = ''
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<style lang="scss" scoped>
.detail-container{
  width: 600px;
  margin: 30px auto;
  ::v-deep .el-input{
    width: 400px;
  }
}
</style>
